/*************************************************************************
	> File Name: word_sort.cc
	> Author: likang
	> Mail:  
	> Created Time: 2014年12月24日 星期三 09时48分18秒
 ************************************************************************/

/*************************************************************************
	本程序输出给定输入中长度为6或超过6的字符串的个数,并输出元素
*************************************************************************/


#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<functional>

using namespace std;

bool isShorter(const string &s1, const string &s2) {
	return s1.size() < s2.size();
}
/***********************************************************
 *函数对象的使用
 **********************************************************/

class GT_cls {
public:
	GT_cls(size_t val = 0): bound(val) {}
	bool operator() (const string& s) { return s.size() >= bound;}
private:
	string::size_type bound;
};

bool GT6(const string &s1) {
	return s1.size() >= 6;
}

int main(int argc, char **argv) {
	vector<string> words;               //store words from file
	if (argc < 2) {
		cout << "please input filename!" << endl;
		return EXIT_FAILURE;
	}
	ifstream infile(argv[1]);
	if (!infile) {
		cout << argv[1] << " does not exit !" << endl;
		return EXIT_FAILURE;
	}
	string word;
	while (infile >> word)
		words.push_back(word);
	//sort usage
	sort(words.begin(),words.end());     //sort the word
	//unique usage
	vector<string>::iterator end_unique = unique(words.begin(),words.end());
	words.erase(end_unique,words.end());
	//stable_sort usage
	stable_sort(words.begin(),words.end(),isShorter);
	//count_if usage
	//1.
	//vector<string>::size_type wc = count_if(words.begin(),words.end(),GT_cls(4));
	/********************************************************************************************************
	 * 2.通过标准库提供的函数对象来查找指定长度的单词
	 *
	 * vector<int> ivec;
	 * for (vector<string>::iterator it = words.begin(); it != words.end(); ++it) 
	 *	 ivec.push_back((*it).size());
	 * vector<string>::size_type wc = count_if(ivec.begin(),ivec.end(),bind1st(less_equal<int>(), 8));
	*********************************************************************************************************/
	cout << wc << " " << "word(s)" << " 8 characters or longer" << endl;
	for (vector<string>::reverse_iterator it = words.rbegin(); it != words.rend() && wc > 0; ++it,--wc) {
		cout << *it << ":" << (*it).size() << "  ";
	}
	cout << endl;
	for (vector<string>::iterator it = words.begin(); it != words.end(); ++it) {
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}
